//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "server\fn_serverExecutePickup.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_object","_action","_parameters","_item_desc","_action_title","_pickup_msg","_shared_variable","_shared_variable_value","_pickup_code","_player","_obj_var_picked","_act_args","_i"];
DEFAULT_PARAM(_object,TMF_EXECUTE_ACTION_OBJECT,objNull);
DEFAULT_PARAM(_caller,TMF_EXECUTE_ACTION_CALLER,objNull);
DEFAULT_PARAM(_act_id,TMF_EXECUTE_ACTION_ID,-1);
DEFAULT_PARAM(_arguments,TMF_EXECUTE_ACTION_ARGS,[]);
DEFAULT_PARAM(_action,TMF_EXECUTE_ACTION_ACTION,[]);

_action_id = _arguments select TMF_EXECUTE_ACTION_PARAM_ACTID;

if (!isnull _object) then {
	if (_action_id > 0) then {
		_instance_id = [_object] call FFUNC(getInstanceID);
		if (count _action  <= 0) then {
			_action = [_object,_action_id] call FFUNC(actionGet);
		};
		TRACE_1("Action",_action);
		_parameters = [_action] call FFUNC(actionGetParams);
		
		_obj_var = format["mission_object_pickup_player_%1",_action_id];
		_player = OGETVAR(_object,_obj_var,objNull);
		if (isnull _player) then {
			OSETVARP(_object,_obj_var,_caller);
			_player = _caller;
			_item_desc = [_parameters, "ITEM_DESC", ([_object] call CFUNC(displayName))] call CFUNC(hashGet);
			_pickup_msg = [_parameters, "PICKUP_MSG", []] call CFUNC(hashGet);
			_shared_variable = [_parameters, "SHARED_VAR", ""] call CFUNC(hashGet);
			_initial_status = false; //[_parameters, "SHARED_VAR_VAL", false] call CFUNC(hashGet);
			_action_title = [_parameters, "PICKUP_TITLE", "STR_MF_PICKUP_DEFACTION"] call CFUNC(hashGet);
			_pickup_code = [_parameters, "PICKUP_CODE", ""] call CFUNC(hashGet);

			_is_picked_up = !_initial_status;
			_obj_var_picked = format["mission_object_pickedup_%1",_action_id];
			OSETVARP(_object,_obj_var_picked,_is_picked_up);
			
			if (IS_STRING(_pickup_msg)) then {_pickup_msg = [_pickup_msg];};
			for [{_i = 0},{_i < count _pickup_msg},{_i = _i + 1}] do {
				if (IS_STRING((_pickup_msg select _i))) then {
					if ((_pickup_msg select _i) == "__CALLER__") then {
						_pickup_msg set [_i, ([_caller,"STR_MF_APLAYER"] call CFUNC(playerName))];
					};
				};
			};
			
			If (count _pickup_msg > 0) then {[QGVAR(all_message),[_pickup_msg,"all",objNull,"hq"],THIS_FILE_] call NFUNC(callEvent);};

			if (!isNil "_shared_variable") then {
				if (_shared_variable != "") then {
					[_instance_id,_shared_variable,_is_picked_up] call FFUNC(setMissionJIPVar);
					[QGVAR(all_var), [_instance_id,_shared_variable,_is_picked_up],THIS_FILE_] call NFUNC(callEvent);
				};
			};
			if (_pickup_code != "") then {
				[QGVAR(all_execute),[_pickup_code,_object,_action],THIS_FILE_] call NFUNC(callEvent);
			};
			TRACE_1("Object Picked Up",ARR_3(_caller, _object, _action_id));

			// This will move the object along with the player.
			// If the player dies or drops the object, it will be where the player died or dropped the object.
			[_object, _caller, _action_id] spawn {
				private ["_object","_player","_action_id","_has_object","_obj_var_picked","_obj_var","_2d_pos","_message","_params","_player_name","_new_pos"];
				DEFAULT_PARAM(_object,0,objNull);
				DEFAULT_PARAM(_player,1,objNull);
				DEFAULT_PARAM(_action_id,2,-1);
				_player_name = [_player] call CFUNC(playerName);
				_has_object = true;
				_obj_var_picked = format["mission_object_pickedup_%1",_action_id];
				_obj_var = format["mission_object_pickup_player_%1",_action_id];
				TRACE_1("Begin Follow Player",ARR_3(_player_name, _object, _action_id));
				_object hideObject true;
				while { _has_object  && alive _player} do {
					// Object is moved only if the player is on the ground and not at sea
					if ((((getPos _player) select 2) < 2) && (vehicle _player == _player)) then {
						_2d_pos = [((getPos _player) select 0),((getPos _player) select 1)];
						if (!SurfaceIsWater _2d_pos) then {
							_new_pos = (_player modelToWorld [0,-1.2,0]);
							_new_pos set [2, (getPosATL _player) select 2];
							_object setPosATL _new_pos;
						};
					};
					sleep 2.645678;
					_has_object = (OGETVAR(_object,_obj_var_picked,false));
				};
				_new_pos = (getPos _object) findEmptyPosition[ 1 , 20];
				if (count _new_pos > 0) then {
					if ((_new_pos distance [0,0,0]) > 1) then {
						_object setPos _new_pos;
					};
				};
				_object hideObject false;
				TRACE_1("Stop Follow Player",ARR_4(_player_name, _object, _action_id, ([_object] call CFUNC(GPSCoords))));
				if (!alive _player || isnull _player) then {
					_message = format["Player Died Carrying %1 (%2) at position %3",typeof _object, _action_id, getPos _object];
					TLOG(_message);
					_act_args = [];
					_act_args set [TMF_EXECUTE_ACTION_PARAM_ACTID, _action_id];
					_act_args set [TMF_EXECUTE_ACTION_PARAM_FUNC, "Drop"];
					_act_args set [TMF_EXECUTE_ACTION_PARAM_TARGET, _object];
					_params = [_object, _player_name, -1, _act_args];
					[QGVAR(all_action), _params,THIS_FILE_] call NFUNC(callEvent);
				};
			};
		} else {
			ERROR_2("Someone already picked up",_player,_caller);
		};
	};
};		
TRACE("End");
TMFPROFILERSTOP;